package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 取款界面
 */
public class GetPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon saveImg = new ImageIcon("images/buttonSure.jpg");
    private JLabel saveLabel = new JLabel("取款金额：");
    private JTextField saveTextField = new JTextField();
    private Font font = new Font("黑体", Font.BOLD, 30);
    private JButton saveBtn = new JButton(saveImg);

    public GetPanel(ATMFrame atmFrame) {
        this.atmFrame = atmFrame;
        this.setLayout(null);
        saveLabel.setBounds(300, 200, 180, 50);
        saveLabel.setFont(font);

        saveTextField.setBounds(440, 200, 180, 50);
        saveTextField.setFont(font);

        saveBtn.setBounds(442, 260, 140, 50);
        saveBtn.addActionListener(this);
        this.add(saveLabel);
        this.add(saveTextField);
        this.add(saveBtn);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == saveBtn) {
            User user = atmFrame.getLoginUser();
            try {
                Double.parseDouble(saveTextField.getText());
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(null, "必须输入数字");
                return;
            }
            if (Double.parseDouble(saveTextField.getText()) <= user.getBlance()) {
                user.setBlance(user.getBlance() - Double.parseDouble(saveTextField.getText()));
                atmFrame.switchPanel("menu");
            } else {
                JOptionPane.showMessageDialog(null, "你的余额不足！");
                return;
            }
        }
    }
}
